﻿//创建时间: 2010-10-14
//创建作者: 李 城
//功能说明: 内容模型【字段排序】 
//最后修改: 2010-10-14
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Model_field_sort : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_ContentModel;
        }
        #endregion

        #region 页面变量
        protected int m_cid = 0;
        protected string m_Title = "";
        protected ModelInfo m_ModelInfo = new ModelInfo();
        protected ModelManage m_ModelManage = new ModelManage();
        protected ModelFieldManage m_ModelFieldManage = new ModelFieldManage();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            base.AlertForNoPermissionControl(TRPOP.CMS.Plugin.VersionLimit.ContentModelEdit);
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }
        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnup_Click(object sender, EventArgs e)
        {
            m_Title = ((ModelInfo)GetPageViewState("modelinfo")).Name;
            if (lbfiled.SelectedIndex == 0)
                return;
            else
            {
                int index = lbfiled.SelectedIndex;
                SetOrder(m_ModelFieldManage.GetInfoById(lbfiled.SelectedValue.ToInt()),
                         m_ModelFieldManage.GetInfoById(lbfiled.Items[index - 1].Value.ToInt()));
                BindData();
                lbfiled.SelectedIndex = -1;
                lbfiled.Items[index - 1].Selected = true;
            }
        }
        /// <summary>
        /// 下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btndown_Click(object sender, EventArgs e)
        {
            m_Title = ((ModelInfo)GetPageViewState("modelinfo")).Name;
            if (lbfiled.SelectedIndex >= lbfiled.Items.Count - 1)
                return;
            else
            {
                int index = lbfiled.SelectedIndex;
                SetOrder(m_ModelFieldManage.GetInfoById(lbfiled.SelectedValue.ToInt()),
                         m_ModelFieldManage.GetInfoById(lbfiled.Items[index + 1].Value.ToInt()));
                BindData();
                lbfiled.SelectedIndex = -1;
                lbfiled.Items[index + 1].Selected = true;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ExecuteJs("parent.reset();");
        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("cid"), out m_cid);
            m_ModelInfo = m_ModelManage.GetInfoById(m_cid);
            if (m_cid <= 0 || m_ModelInfo == null)
            {
                Alert("请通过正确的途径进入本页面");
                return;
            }
            else
            {
                m_Title = m_ModelInfo.Name + "";
                InitPageViewState("modelinfo", m_ModelInfo);
            }
        }

        public void BindData()
        {
            int.TryParse(GetRequestViewState("cid"), out m_cid);
            lbfiled.DataSource = m_ModelFieldManage.GetListByCondition(0, " and ModelId=" + m_cid.ToString() + " ", "DisplayOrder,ModelFieldId");
            lbfiled.DataTextField = "Name";
            lbfiled.DataValueField = "ModelFieldId";
            lbfiled.DataBind();
            if (lbfiled.Items.Count <= 1)
            {
                btnup.Enabled = false;
                btndown.Enabled = false;
            }
            if (lbfiled.SelectedIndex <= 0 && lbfiled.Items.Count > 0)
            {
                lbfiled.SelectedIndex = -1;
                lbfiled.SelectedIndex = 0;
            }
        }
        public void SetOrder(ModelFieldInfo oinfo1, ModelFieldInfo oinfo2)
        {
            if (oinfo1 != null && oinfo2 != null)
            {
                if (oinfo1.DisplayOrder == 0)
                {
                    oinfo1.DisplayOrder = oinfo1.ModelFieldId;
                    m_ModelFieldManage.Save(oinfo1);
                }
                if (oinfo2.DisplayOrder == 0)
                {
                    oinfo2.DisplayOrder = oinfo2.ModelFieldId;
                    m_ModelFieldManage.Save(oinfo2);
                }
                base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.ModelFieldStor, oinfo1.ModelId.ToString());
                int tempOrder = oinfo1.DisplayOrder;
                oinfo1.DisplayOrder = oinfo2.DisplayOrder;
                oinfo2.DisplayOrder = tempOrder;
                m_ModelFieldManage.Save(oinfo1);
                m_ModelFieldManage.Save(oinfo2);
            }
        }
        #endregion

    }
}
